package controller;

import java.util.ArrayList;
import java.util.Collection;

import model.Accumulator;
import model.Register;

public class RuntimeState {
	private int pointer;
	private int accumulator;
	private ArrayList<Register> register = new ArrayList<Register>();

	public int getPointer() {
		return pointer;
	}

	public void setPointer(int pointer) {
		this.pointer = pointer;
	}

	public int getAccumulator() {
		return accumulator;
	}

	public void setAccumulator(int accumulator) {
		this.accumulator = accumulator;
	}

	public Collection<Register> getMemory() {
		return register;
	}

	public void setMemory(Collection<Register> register) {
		this.register.addAll(register);
	}

	public int getRegisterValueAt(int index) {
		Register register = this.register.get(index);
		if (register != null) {
			return register.getValue();
		}
		return 0;
	}

	public void setRegisterValueAt(int index, int value) {
		if (register.size() < index) {
			for (int i = register.size(); i <= index; i++) {
				Register nreg = MemoryController.buildRegister();
				nreg.setValue(0);
				register.add(nreg);
			}
		}
		Register register = this.register.get(index);
		register.setValue(value);
	}

	public static RuntimeState buildCurrentRuntimeState() {
		RuntimeState state = new RuntimeState();
		state.setAccumulator(Accumulator.getInstance().getValue());
		state.setPointer(MemoryController.getInstance().getPointer());
		state.setMemory(MemoryController.getInstance().getCopyOfRegister());
		return state;

	}
}
